package com.ruoyi.project.dict.approve.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.TableSplitUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.dict.approve.domain.CiSentenceApprove;
import com.ruoyi.project.dict.approve.mapper.CiSentenceApproveMapper;
import com.ruoyi.project.dict.approve.service.ICiSentenceApproveService;
import com.ruoyi.project.dict.dictsentence.domain.CiSentence;
import com.ruoyi.project.dict.dictsentence.mapper.CiSentenceMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 造句审核Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-10-20
 */
@Service
@DataSource(DataSourceType.DICT)
public class CiSentenceApproveServiceImpl implements ICiSentenceApproveService {

    protected static Log logger = LogFactory.getLog(CiSentenceApproveServiceImpl.class);


    @Autowired
    private CiSentenceApproveMapper ciSentenceApproveMapper;

    @Autowired
    private CiSentenceMapper ciSentenceMapper;


    /**
     * 查询造句审核
     *
     * @param id 造句审核ID
     * @return 造句审核
     */
    @Override
    public CiSentenceApprove selectCiSentenceApproveById(Long id) {
        return ciSentenceApproveMapper.selectCiSentenceApproveById(id);
    }

    /**
     * 查询造句审核列表
     *
     * @param ciSentenceApprove 造句审核
     * @return 造句审核
     */
    @Override
    public List<CiSentenceApprove> selectCiSentenceApproveList(CiSentenceApprove ciSentenceApprove) {
        return ciSentenceApproveMapper.selectCiSentenceApproveList(ciSentenceApprove);
    }

    /**
     * 新增造句审核
     *
     * @param ciSentenceApprove 造句审核
     * @return 结果
     */
    @Override
    public int insertCiSentenceApprove(CiSentenceApprove ciSentenceApprove) {
        return ciSentenceApproveMapper.insertCiSentenceApprove(ciSentenceApprove);
    }

    /**
     * 修改造句审核
     *
     * @param ciSentenceApprove 造句审核
     * @return 结果
     */
    @Override
    public int updateCiSentenceApprove(CiSentenceApprove ciSentenceApprove) {
        int ret = ciSentenceApproveMapper.updateCiSentenceApprove(ciSentenceApprove);
        logger.info("auto approve -> " + ciSentenceApprove.getAutoApprove());
        if (ciSentenceApprove.getAutoApprove() == 1) {
            this.approve(ciSentenceApprove.getId(), 1);
        }
        return ret;
    }

    @Override
    @Transactional
    public int approve(Long id, Integer status) {
        CiSentenceApprove ciSentenceApprove = ciSentenceApproveMapper.selectCiSentenceApproveById(id);
        ciSentenceApprove.setApproveSt(status);
        ciSentenceApprove.setApproveTime(DateUtils.getNowDate());
        ciSentenceApprove.setApproveUser(ShiroUtils.getLoginName());
        if (status == 1) {
            CiSentence ciSentence = new CiSentence();
            ciSentence.setCi(ciSentenceApprove.getCi());
            ciSentence.setContent(ciSentenceApprove.getContent());
            ciSentence.setCreatedTime(ciSentenceApprove.getCreatedTime());
            ciSentence.setKkid(ciSentenceApprove.getKkid());
            ciSentence.setFromType(1L);
            ciSentence.setTable(TableSplitUtil.getCiSentenceTable(ciSentenceApprove.getCi()));
            ciSentence.setAppId(ciSentenceApprove.getAppId());
            ciSentenceMapper.insertCiSentence(ciSentence);
        }
        return ciSentenceApproveMapper.updateCiSentenceApprove(ciSentenceApprove);
    }

    /**
     * 删除造句审核对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCiSentenceApproveByIds(String ids) {
        return ciSentenceApproveMapper.deleteCiSentenceApproveByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除造句审核信息
     *
     * @param id 造句审核ID
     * @return 结果
     */
    @Override
    public int deleteCiSentenceApproveById(Long id) {
        return ciSentenceApproveMapper.deleteCiSentenceApproveById(id);
    }
}
